package com.example.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class RxJavaMainActivity extends FragmentActivity {

    private static final String TAG = "RxJavaMainActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Observable<JSONObject> observable = Observable.create(new ObservableOnSubscribe<JSONObject>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<JSONObject> emitter) throws Exception {
                JSONObject jsonObject = new JSONObject();
                emitter.onNext(jsonObject);
            }
        }).doOnNext(new Consumer<JSONObject>() {
            @Override
            public void accept(JSONObject jsonObject) {
                Log.i(TAG, "doNext生成文本成功");
            }
        }).doOnError(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Log.i(TAG, "doNext生成文本失败:" + throwable.getMessage());
            }
        }).flatMap(new Function<JSONObject, ObservableSource<JSONObject>>() {
            @Override
            public ObservableSource<JSONObject> apply(@NonNull JSONObject jsonObject) throws Exception {
                Observable observable1 = Observable.create(new ObservableOnSubscribe<JSONObject>() {
                    @Override
                    public void subscribe(@NonNull ObservableEmitter<JSONObject> emitter) throws Exception {
                        emitter.onNext(jsonObject);
                    }
                });
                return observable1;
            }
        }).doOnNext(new Consumer<JSONObject>() {
            @Override
            public void accept(JSONObject jsonObject) {
                Log.i(TAG, "doNext加载文本成功");
            }
        }).doOnError(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Log.i(TAG, "doNext加载文本失败:" + throwable.getMessage());
            }
        });

        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Consumer<JSONObject>() {
                            @Override
                            public void accept(JSONObject jsonObject) {
                                Log.i(TAG, "文本加载成功");
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) {
                                Log.e(TAG, "文本加载失败: " + throwable.getMessage());
                            }
                        }
                );
    }

    private void ddd(Observable<JSONObject> observable) {
        observable.flatMap(new Function<JSONObject, ObservableSource<JSONObject>>() {
            @Override
            public ObservableSource<JSONObject> apply(@NonNull JSONObject jsonObject) throws Exception {
                Observable observable1 = Observable.create(new ObservableOnSubscribe<JSONObject>() {
                    @Override
                    public void subscribe(@NonNull ObservableEmitter<JSONObject> emitter) throws Exception {
                        emitter.onNext(jsonObject);
                    }
                });
                return observable1;
            }
        }).doOnNext(new Consumer<JSONObject>() {
            @Override
            public void accept(JSONObject jsonObject) {
                Log.i(TAG, "doNext加载文本成功222222");
            }
        }).doOnError(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Log.i(TAG, "doNext加载文本失败22222:" + throwable.getMessage());
            }
        });
    }
}
